//
//  CCVSGraphData.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 5/2/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCVSGraphData.h"
#import "CommonAPI.h"
#import "CCVSCommon.h"
#import "CCVSGraphData+Utilities.h"


@implementation CCVSGraphData

@synthesize customViewType = _customViewType;
@synthesize isExpanded = _isExpanded;

@synthesize chartType = _chartType;
@synthesize chartTitle = _chartTitle;
@synthesize chartArray = _chartArray;

-(NSString*)customViewType
{
    if (!_customViewType) _customViewType = @"CCVSCorePlotGraphView";
    return _customViewType;
}

-(void)setChartArray:(NSArray *)chartArray
{
    NSArray *arrayToBeSorted = [chartArray copy];
    
    if ([arrayToBeSorted count] < 2) {
        _chartArray = arrayToBeSorted;
    } else {
        
        _chartArray = [arrayToBeSorted sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* dict1, NSDictionary* dict2) {
            NSArray *keys1 = [dict1 allKeys];
            NSArray *keys2 = [dict2 allKeys];
            if ([keys1 count] && [keys2 count]) {
                NSString *string1 = keys1[0];
                NSString *string2 = keys2[0];
            
                return [string1 compare:string2 options:NSCaseInsensitiveSearch];
            } else {
                return (NSComparisonResult)NSOrderedSame;
            }
        }];
    }
}

-(NSArray *)sortDataArray:(NSArray *)dataArray
{
    NSArray *arrayToBeSorted = [dataArray copy];
    if ([arrayToBeSorted count] < 2) {
        return arrayToBeSorted;
    }
    
    return [arrayToBeSorted sortedArrayUsingComparator:^NSComparisonResult(id first, id second) {
        NSDictionary *dict1 = (NSDictionary *)first;
        NSDictionary *dict2 = (NSDictionary *)second;
        NSArray *keys1 = [dict1 allKeys];
        NSArray *keys2 = [dict2 allKeys];
        if ([keys1 count] && [keys2 count]) {
            NSString *string1 = keys1[0];
            NSString *string2 = keys2[0];
            
            return [string1 compare:string2 options:NSCaseInsensitiveSearch];
        } else {
            return (NSComparisonResult)NSOrderedSame;
        }
    }];
}


#pragma mark -
#pragma mark CPTBarPlot delegate method

-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
{
    NSLog(@"barWasSelectedAtRecordIndex %d", index);
}

#pragma mark -
#pragma mark Plot Data Source Methods


-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    int count = 0;
    
        NSArray *dataArray = [CCVSGraphData removeTotalFromArray:self.chartArray];
        count = [dataArray count];

    return count;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    
    NSDecimalNumber *num = nil;
    NSArray *dataArray = nil;
    Class cls = [CommonAPI class];
    if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
        switch ( fieldEnum ) {
            case CPTBarPlotFieldBarLocation:
                if (index == 0)
                    num = (NSDecimalNumber *) [NSDecimalNumber numberWithInt:1];
                else
                    num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index*5];
                break;
                
            case CPTBarPlotFieldBarTip:
                    dataArray = [CCVSGraphData removeTotalFromArray:[self sortDataArray:self.chartArray]];
                    num = (NSDecimalNumber *)[cls getValue:index graphDataArray:dataArray];
                break;
        }
    }
    else if ( [plot isKindOfClass:[CPTPieChart class]] ) {
        dataArray = [CCVSGraphData removeTotalFromArray:[self sortDataArray:self.chartArray]];
        if (index >= [dataArray count])
            return nil;
        
        if ( fieldEnum == CPTPieChartFieldSliceWidth ) {
            num = (NSDecimalNumber *)[cls getValue:index graphDataArray:dataArray];
        }
        else {
            num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
        }
    }
    
    return num;
}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
    
    static CPTMutableTextStyle *whiteText = nil;
    if ( !whiteText ) {
        whiteText       = [[CPTMutableTextStyle alloc] init];
        whiteText.color = [CPTColor whiteColor];
        whiteText.fontName = @"Arial";
        whiteText.fontSize = plot.frame.size.width/40;
        if (whiteText.fontSize < 8)
            whiteText.fontSize = 8;
    }
    NSArray *dataArray = [CCVSGraphData removeTotalFromArray:[self sortDataArray:self.chartArray]];
    
    unsigned long l = [[CommonAPI getValue:index graphDataArray:dataArray] longValue];

    CPTTextLayer *newLayer = nil;
    
    if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
        newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", (unsigned long)l] style:whiteText];
    }
    else if ( [plot isKindOfClass:[CPTPieChart class]] ) {
        NSString *label;
        if ([[[dataArray objectAtIndex:index] allKeys] count]) {
            label = [[dataArray objectAtIndex:index] allKeys][0];
        } else {
            label = @"";
        }
        newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@[%lu]", label, (unsigned long)l] style:whiteText];
    }
    
    return newLayer;
}

@end
